package com.gin.kotlin.no_gay_alliance.provider.ngaphp

import com.gin.kotlin.no_gay_alliance.argument.ItemArgument
import com.gin.kotlin.no_gay_alliance.model.ItemDataModel
import com.gin.nga.main.NgaApi
import com.gin.nga.response.base.SingleData
import com.gin.nga.response.item.ItemData
import java.lang.ref.WeakReference

/**
 * 道具商店提供者
 * @property dir String
 */
class ItemStoreProvider private constructor() : AbstractCachedDataProvider<ItemArgument, SingleData<ItemData>, ItemDataModel>(
    javaType = TYPE_FACTORY.constructType(ItemDataModel::class.java)
) {
    override val dir = "ITEM_STORE"

    override fun transform(rawData: SingleData<ItemData>) = rawData.data?.let { ItemDataModel(it) } ?: ItemDataModel()

    override fun requestMethod(ngaApi: NgaApi, params: ItemArgument?) = ngaApi.itemApi.store(
        typeId = params?.type?.id,
        page = params?.page,
        subTypeId = params?.subTypeId
    )

    override fun obtainRelativePath(userId: Long, params: ItemArgument?): String {
        val (type, page, subTypeId) = params!!
        return "${type}_page_${page}_${subTypeId}.json"
    }

    companion object {
        private var instance: WeakReference<ItemStoreProvider> = WeakReference(ItemStoreProvider())
        fun getInstance(): ItemStoreProvider = instance.get() ?: ItemStoreProvider().also { instance = WeakReference(it) }
    }
}